package com.fysports.web.controller.system;

import com.jfinal.json.Json;
import com.fysports.web.controller.AbstractBaseController;
import com.fysports.web.kit.commons.PathBizKit;
import com.fysports.web.kit.commons.StringKit;
import com.fysports.web.kit.tree.ITreeBuilder;
import com.fysports.web.kit.tree.SystemMenuTreeBuilder;
import com.fysports.web.kit.tree.SystemRoleTreeBuilder;
import com.fysports.web.kit.tree.TreeNode;
import com.fysports.web.model.system.SystemMenu;
import com.fysports.web.model.system.SystemRole;
import com.fysports.web.module.view.JsonView;
import com.fysports.web.module.view.OperateType;

import java.io.UnsupportedEncodingException;
import java.util.List;


/**
 * 权限管理
 * Created by konbluesky
 * Date : 15/7/29 下午11:31
 * Project :jdmdata_server
 */
public class ManagerRoleController extends AbstractBaseController {

    public void index() {

    }


    public void set() {
        moduleFactory.create(this);
        execute();
        //角色树
        ITreeBuilder<SystemRole> builder = new SystemRoleTreeBuilder();
        List<TreeNode<SystemRole>> roles = builder.builderTree(SystemRole.dao.searchAll());
        setAttr("rolejson", Json.getJson().toJson(roles));
        //菜单树
        ITreeBuilder<SystemMenu> menuTreeBuilder = new SystemMenuTreeBuilder();
        List<TreeNode<SystemMenu>> menus =
                menuTreeBuilder.builderTree(SystemMenu.dao.search(SystemMenu.COL_LIVEFLAG, 1));
        setAttr("menujson", Json.getJson().toJson(menus));
        render(PathBizKit.getPageAdminPath("/system/role/role_list.ftl"));
    }


    /**
     * 保存新增节点
     */
    public void savenode() {
        boolean status = false;
        try {
            String puuid = getPara("puuid", "");
            String rolename = getPara("rolename", "temp");
            SystemRole sr = new SystemRole();
            sr.set(SystemRole.COL_ROLENAME, rolename);
            sr.set(SystemRole.COL_PARENTUUID, puuid);
            status = sr.save();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        JsonView json = new JsonView();
        json.setStatus(status);
        json.setMsg(status ? OperateType.SAVE_SUCCESS : OperateType.SAVE_FAIL);
        setAttr("jsonView", json);
        //保存成功后返回set界面
        set();
        render(PathBizKit.getPageAdminPath("/system/role/role_list.ftl"));
    }


    public void delete() {
        String uuid = getPara();
        SystemRole sr = SystemRole.dao.findById(uuid);
        JsonView json = new JsonView();
        boolean status = false;
        if (sr != null) {
            status = sr.delete();
        }
        json.setStatus(status);
        json.setMsg(status ? OperateType.OPERATE_SUCCESS : OperateType.OPERATE_FAIL);
        renderJson(json);
    }


    public void update() throws UnsupportedEncodingException {
        String uuid = getPara("uuid");
        String rolename = getPara("rolename");
        SystemRole sr = SystemRole.dao.findById(uuid);
        JsonView json = new JsonView();
        boolean status = false;
        if (sr != null) {
            sr.set(SystemRole.COL_ROLENAME, rolename);
            status = sr.update();
        }
        json.setStatus(status);
        json.setMsg(status ? OperateType.OPERATE_SUCCESS : OperateType.OPERATE_FAIL);
        renderJson(json);
    }


    /**
     * 根据roleid 获取对应menu菜单
     */
    public void menu() {
        String roleid = getPara("roleid", "xxxx");
        SystemRole sr = SystemRole.dao.findById(roleid);
        boolean status = false;
        JsonView json = new JsonView();
        if (sr != null) {
            List<SystemMenu> list = sr.getRoleMenu();
            //取得结果
            if (list.size() > 0) {
                StringBuilder sb = new StringBuilder();
                //拼接 id1,id2,id3
                for (SystemMenu sm : list) {
                    sb.append(",");
                    sb.append(sm.getStr("code"));
                }
                if (sb.toString().length() > 0) {
                    status = true;
                    json.setMapEntry("menus", sb.toString().substring(1));
                }
            }
        }
        json.setStatus(status);
        json.setMsg(status ? OperateType.OPERATE_SUCCESS : OperateType.OPERATE_FAIL);
        renderJson(json);
    }


    public void savemenu() {
        String roleid = getPara("roleid", "xxxx");
        String menus = getPara("menus", "");
        SystemRole sr = SystemRole.dao.findById(roleid);
        boolean status = false;
        JsonView json = new JsonView();
        if (sr != null && StringKit.notBlank(menus)) {
            status = sr.addMenuList(menus.split(","));
        }
        json.setStatus(status);
        json.setMsg(status ? OperateType.OPERATE_SUCCESS : OperateType.OPERATE_FAIL);
        renderJson(json);
    }
}
